﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports WSFactory

Public Class CajasDAL
    Public Function registraCajas(ByVal pCaja As CajasBE) As Integer
        Dim instrSql As String
        Dim oInsParm(0 To 6) As OdbcParameter
        Try

            instrSql = "INSERT INTO ""Cajas""(""IdEmpresa"", ""IdC"", ""Id"", ""Nombre"", ""Status"", ""Cobra"", ""Clave"" " & _
                                        "VALUES (?,?,?,?,?,?,?);"

            oInsParm(0) = New OdbcParameter("", pCaja.IdEmpresa)
            oInsParm(1) = New OdbcParameter("", pCaja.IdC)
            oInsParm(2) = New OdbcParameter("", pCaja.Id)
            oInsParm(3) = New OdbcParameter("", pCaja.Nombre)
            oInsParm(4) = New OdbcParameter("", pCaja.Status)
            oInsParm(5) = New OdbcParameter("", pCaja.Cobra)
            oInsParm(6) = New OdbcParameter("", pCaja.Clave)


            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oInsParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        Finally
            oInsParm = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function modificacajas(ByVal pCaja As CajasBE) As Boolean
        Dim instrsql As String
        Dim oUpdParm(0 To 4) As OdbcParameter
        Try
            instrsql = "UPDATE ""cajas"" " & _
                                        "SET ""Clave"" = ?, " & _
                                                """Cobra"" = ?, " & _
                                                """Nombre"" = ?, " & _
                                                """Status"" = ? " & _
                                                "WHERE ""Id"" = ? " & _
                                            "AND ""IdEmpresa"" = ?;"

            oUpdParm(0) = New OdbcParameter("", pCaja.Clave)
            oUpdParm(1) = New OdbcParameter("", pCaja.Cobra)
            oUpdParm(2) = New OdbcParameter("", pCaja.Nombre)
            oUpdParm(3) = New OdbcParameter("", pCaja.Status)
            oUpdParm(4) = New OdbcParameter("", pCaja.Id)
            oUpdParm(5) = New OdbcParameter("", pCaja.IdEmpresa)
            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrsql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oUpdParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
            
        Catch ex As Exception
            Throw
        Finally
            oUpdParm = Nothing
            instrsql = Nothing
        End Try
    End Function

    Public Function getCajas(ByVal pIdEmpr As Integer, ByVal pStrCaja As String) As List(Of CajasBE)
        Dim instrSql As String
        Dim cajaLs As List(Of CajasBE) = New List(Of CajasBE)
        Try
            instrSql = "SELECT ""IdEmpresa"", ""IdC"", ""Id"", ""Nombre"", ""Status"", ""Cobra"", ""Clave"" " & _
                                     "FROM ""Cajas"" " & _
                                    "WHERE ""IdEmpresa"" = ? " & _
                                        "AND ""Nombre"" LIKE '%" & pStrCaja & "%' ;"

            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As OdbcCommand = New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Add(New OdbcParameter("", pIdEmpr))
                    myConn.Open()
                    Using myDR As OdbcDataReader = myComm.ExecuteReader()
                        While myDR.Read()
                            Dim cajas As CajasBE = cajasFact.Build(myDR)
                            If (Not cajas Is Nothing) Then
                                cajaLs.Add(cajas)
                            End If
                            cajas = Nothing
                        End While
                    End Using
                End Using
            End Using
            Return cajaLs
            
        Catch ex As Exception
            Throw 
        Finally
            cajaLs = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function getCajasByEmpr(ByVal pIdEmpr As Integer) As List(Of CajasBE)
        Dim instrSql As String
        Dim cajaLs As List(Of CajasBE) = New List(Of CajasBE)
        Try
            instrSql = "SELECT ""IdEmpresa"", ""IdC"", ""Id"", ""Nombre"", ""Status"", ""Cobra"", ""Clave"" " & _
                                     "FROM ""Cajas"" " & _
                                    "WHERE ""IdEmpresa"" = ?;"

            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As OdbcCommand = New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Add(New OdbcParameter("", pIdEmpr))
                    myConn.Open()
                    Using myDR As OdbcDataReader = myComm.ExecuteReader()
                        While myDR.Read()
                            Dim caja As CajasBE = cajasFact.Build(myDR)
                            If (Not caja Is Nothing) Then
                                cajaLs.Add(caja)
                            End If
                            caja = Nothing
                        End While
                    End Using
                End Using
            End Using
            Return cajaLs
            
        Catch ex As Exception
            Throw 
        Finally
            cajaLs = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function existeCaja(ByVal pCaja As String, ByVal pIdEmpr As Integer) As Boolean
        Dim existe As Boolean = False
        Dim instrSql As String
        Dim oSelParm(0 To 1) As OdbcParameter
        Try

            instrSql = "SELECT COUNT(*) " & _
                                    "FROM ""Cajas"" " & _
                                    "WHERE ""IdEmpresa"" = ? " & _
                                        "AND ""Nombre"" = ?;"

            oSelParm(0) = New OdbcParameter("", pIdEmpr)
            oSelParm(1) = New OdbcParameter("", pCaja)
            Dim count As Object = db.ExecScalar(instrSql, CommandType.Text, oSelParm, My.Settings.cs)
            If (Not count Is Nothing) Then
                If (CShort(count) > 0) Then
                    existe = True
                Else
                    existe = False
                End If
            Else
                existe = False
            End If
            Return existe
            
        Catch ex As Exception
            Throw 
        Finally
            oSelParm = Nothing
            instrSql = Nothing
            existe = Nothing
        End Try
    End Function
End Class
